﻿<!--#Include File = "CustomTemplate.asp" -->

<%
	Call  PageController.RenderPage()
	Public Function Page_Configure()
		PageController.PageTitle = "Content management"
		PageController.AuthorizedRoles =  Array("writer","publisher")
		pageController.RequiresAuthentication  =True
		PageController.PageCSSFiles = "main.css" 		
	End function
%>
<%Public Function Page_RenderForm()%>
	<H2>Content management</H2>
	
	<%	Page.Execute %>
	
	<%txtQuery%> <%cmdSearch%>
	<br/>
	<br/>
	<a href="ContentEdit.asp">Add new</a>
	<HR>
	<%objDataGrid%>	
	<hr>
	<a href="ContentEdit.asp">Add new</a>
	<HR>

	
<%End Function%>



<%  'This would normaly go in a another page, but for the sake of simplicity and to minimize the number of pages
    'I'm including code behind stuff here...
	
	Dim objDataGrid
	Dim txtQuery
	Dim cmdSearch
	
	Dim chkPagerStyle
	Dim SortOrder
	
	

		
	Public Function Page_Init()
			
		Set txtQuery = New_ServerTextBoxEX("txtQuery",100,40)
		Set cmdSearch = New_ServerButton("cmdSearch")		
		
		Set objDataGrid = New_ServerDataGrid("objDataGrid")
		Set chkPagerStyle  = New_ServerCheckBox("chkPagerStyle")	
		
		Page.AutoResetScrollPosition = True		
	End Function

	Public Function Page_Controls_Init()						
		SortOrder = "updatetime"
		cmdSearch.Text = "Search"
		objDataGrid.Pager.ProgressStyle = "font-size:8pt"
		objDataGrid.Pager.PrevNextStyle = "font-size:8pt"
		objDataGrid.Pager.PagerStyle    = "font-size:8pt"	
		objDataGrid.AllowPaging = true
		objDataGrid.Pager.PagerType = 0
		objDataGrid.Pager.PagerSize = 20
		objDataGrid.Pager.CurrentPageStyle = "color:blue;font-weight:bold"	

		objDataGrid.AlternatingItemStyle = "background-color:#dddddd;font-size:8pt;border:1px solid black" 		
		objDataGrid.SortStyle = "color:white"
		objDataGrid.ItemStyle = "font-size:8pt;border:1px solid black"
		objDataGrid.Control.Style = "width:100%;border-collapse:collapse;"
		objDataGrid.HeaderStyle = "font-weight:bold;color:white;background-color:#4682b4;font-size:10pt;font-family:tahoma;border:1px solid black"
		objDataGrid.BorderWidth = 1
		objDataGrid.BorderColor = "Black"
		objDataGrid.AutoGenerateColumns = False 'To avoid the grid control from doing this :-)

	End Function
	
	Public Function Page_LoadViewState()
		SortOrder  = Page.ViewState.GetValue("SortOrder")	
	End Function

	Public Function Page_PreRender()
	
		DoSearch
		Page.ViewState.Add "SortOrder",SortOrder
	End Function
	
	Public Sub DoSearch()		
		'Three ways. I recommend the 3rd as it is more flexble...
		'Dim columns(x) and then do Set columns(x) = New_DataGridColumn(ColumnType,DataTextField,DataValueField,DataFormatString)
		'objDataGrid.GenerateColumns() 'Do it and I will take over...
		objDataGrid.CreateColumns Array("id","title","description","createtime","username","categoryname","status", ""),Array("ID","Title","Description","Create Time","Create User","Category","Status", "Action")
		'Function to render the header

		Set objDataGrid.DataSource = GetRecordSet("Select c.id, c.title, c.description,c.createtime,c.usercreate,c.categoryid,cat.name as categoryname, c.status, u.username from content as c inner join category as cat on c.categoryid = cat.id " &_
		"inner join [user] as u on u.id = c.usercreate Where c.title like N'%"&txtQuery.Text&"%' or c.description like N'%" & txtQuery.Text & "%' and c.isdeleted =0 Order By c." & SortOrder & " DESC")
		
		'Response.Write("Select c.id, c.title, c.description,c.createtime,c.usercreate,c.categoryid,cat.name as categoryname, c.status, u.username from content as c inner join category as cat on c.categoryid = cat.id " &_
		'"inner join [user] as u on u.id = c.usercreate Where c.title like N'%"&txtQuery.Text&"%' or c.description like N'%" & txtQuery.Text & "%' and c.isdeleted =0 Order By c." & SortOrder & " DESC")
		
		
		objDataGrid.HeaderFunctionName = "objDataGrid_Header"
		'objDataGrid.FooterFunctionName = "objDataGrid_Footer"
		
	
		objDataGrid.PagerStyle = "background-color:yellow"
	
		objDataGrid.DataKeyFieldName = "id"
		
		objDataGrid.Columns(6).ColumnType = 3    'Modify the column settings we need.
		objDataGrid.Columns(6).HeaderText = "Status"
		objDataGrid.Columns(6).HorizonalAlign = "center"
		objDataGrid.Columns(6).CellRenderFunctionName = "RenderStatusColumn"
		
		objDataGrid.Columns(7).ColumnType = 3    'Modify the column settings we need.
		objDataGrid.Columns(7).HeaderText = "Actions"
		objDataGrid.Columns(7).HorizonalAlign = "center"
		objDataGrid.Columns(7).CellRenderFunctionName = "RenderActionColumn"

	End Sub
	
	Public Function cmdSearch_OnClick()
		
	
	End Function

	Public Function objDataGrid_ClickColumn0(e)
		objDataGrid.SelectedItemIndex = CInt(e.Instance)
	End Function
	
	Public Function RenderColumn0(ds)
		 Response.Write " <A style='color:black' " 
		 Response.Write Page.GetEventScript("href","objDataGrid","ClickColumn0",ds.AbsolutePosition,"chris")
		 Response.Write " >" & ds(0) & "</a>"
	End Function
	
	Public Function RenderStatusColumn(ds)
		if ds("status").Value = 1 then
			Response.Write("Created")
		elseif ds("status").Value = 2 then
			Response.Write("Pedding")
		elseif ds("status").Value = 3 then
			Response.Write("Allowed publish")
		elseif ds("status").Value = 4 then
			Response.Write("Denied publish")
		end if
	End Function
	
	Public Function RenderActionColumn(ds)
		Response.Write "<A title='Edit Content' HREF='ContentEdit.asp?id=" & ds("id").Value & "' Class='ActionLink'>Edit</A> <br/><br/> "
		Response.Write "<A title='Delete Content' " & Page.GetEventScript("HREF","Page","DeleteContent",ds("id").Value,"") & " Class='ActionLink'>Delete</a> <br/><br/>"	
		if ds("status")=1 and CurrentUser.IsInRole("writer") then
			Response.Write "<A title='Send Request Publish' HREF='Request.asp?id=" & ds("id").Value & "' Class='ActionLink'>Request</A> <br/><br/>"
		end if
		
		if ds("status")=1 and CurrentUser.IsInRole("publisher") then
			Response.Write "<A title='Publish now' " & Page.GetEventScript("HREF","Page","PublishContent",ds("id").Value,"") & " Class='ActionLink'>Publish</a> <br/><br/>"
		end if
		Response.Write "<A title='Setting content relation' HREF='ContentRelation.asp?parentID=" & ds("id").Value & "' Class='ActionLink'>Content Relation</A>"
	End Function
	
	Public Function Page_DeleteContent(e)
		Dim sSQL
		Dim rs
		sSQL = "SELECT * FROM content Where id=" & Replace(e.Instance,"'","''")
		
		Set rs = DBLayer.GetRecordSet(sSQL)		
		If rs.RecordCount = 0 Then
			rs.AddNew
		End If		
		
		rs("isdeleted").Value	= true	
	
		DBLayer.UpdateRecordSet rs
		
	
	End Function
	
	Public Function Page_PublishContent(e)
		Dim sSQL
		Dim rs
		sSQL = "SELECT * FROM content Where id=" & Replace(e.Instance,"'","''")
		
		Set rs = DBLayer.GetRecordSet(sSQL)		
		If rs.RecordCount = 0 Then
			rs.AddNew
		End If		
		
		rs("status").Value	= 3	
	
		DBLayer.UpdateRecordSet rs
		
	
	End Function
	
	
	public Function objDataGrid_ColumnSort(e)
		If e.Instance = "id" Then
			SortOrder  = "id DESC"
		Else
			SortOrder  = "id"
		End If
		objDataGrid.SelectedItemIndex = -1
		objDataGrid.EditItemIndex= -1
	End Function
		
	Public Function RenderFormatedCell(v)
		If "" & v = "" Then
			RenderFormatedCell =  "@@"
		Else
			RenderFormatedCell =  v
		End If
	End Function
	
	
	
	Public Function objDataGrid_Header()
		Response.Write "List of all content"
	End Function
	
	
%>